﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace PhysicalExamination5.WebApi.Controllers
{
    /// <summary>
    /// 创建生成项目实体类
    /// </summary>
    [Route("[controller]")]
    [ApiController]
    public class ClassFileControllersr : ControllerBase
    {

        private readonly ISqlSugarClient db;
        private readonly IWebHostEnvironment _env;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public ClassFileControllersr(ISqlSugarClient db, IWebHostEnvironment env)
        {
            this.db = db;
            _env = env;
        }

        /// <summary>
        /// 根据数据库表生成项目实体类
        /// </summary>
        /// <param name="LeiBie">PhysicalExamination</param>
        /// <returns></returns>
        [HttpGet]
        public string CreateFileClass(string LeiBie)
        {

            try
            {
                //获取当前项目根路径
                var XiangMuGenLuJing = "";
                //替换成我想要的路径
                var ArrLuJing = _env.ContentRootPath.Split(@"\");
                ArrLuJing[ArrLuJing.Length - 2] = "PhysicalExamination5.Model";
                //循环拼接
                foreach (var ArrLuJingJieDian in ArrLuJing)
                {
                    if (string.IsNullOrEmpty(ArrLuJingJieDian))
                    {
                        continue;
                    }
                    XiangMuGenLuJing += ArrLuJingJieDian + @"\";
                }

                if (LeiBie == "PhysicalExamination")
                {
                    XiangMuGenLuJing += @"PhysicalExamination\";
                }
                else
                {
                    return "错误,未找到此类别!";
                }

                //判断文件夹是否存在
                if (!Directory.Exists(XiangMuGenLuJing))
                {
                    Directory.CreateDirectory(XiangMuGenLuJing);
                }

                //根据数据生成全部表
                //db.DbFirst.IsCreateAttribute().CreateClassFile("D:\\工作相关\\YiQiWeChatApi\\YiQiWeChatDemo.Model", "YiQiWeChatDemo.Model");
                //生成指定表                              指定表名                                                文件路径             命名空间
                //出现问题   没有主键标识和自增标识   所以废弃
                //db.DbFirst.Where("WX_DuanXinJiLu", "WX_YongHuDengLu", "WX_YongHuDengLuJiaoYan").CreateClassFile(XiangMuGenLuJing, "YiQiWeChatDemo.Model");

                if (LeiBie == "PhysicalExamination")
                {
                    db.DbFirst.IsCreateAttribute().CreateClassFile(XiangMuGenLuJing, "PhysicalExamination5.Model.PhysicalExamination");
                }

                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }



    }
}
